<?php

namespace app\controller;

use app\BaseController;
use think\Request;
use app\service\SingleService;
use Exception;

class Single extends BaseController
{    
    protected $middleware = [
        \app\middleware\LoginCheck::class
    ];
    protected $request;
    protected $singleService;
    public function __construct(Request $request,SingleService $singleService)
    {   
        $this->request = $request;
        $this->singleService = $singleService;
    }    

    public function list()
    {       
        try {          
            $params = $this->request->param();             
            $data = $this->singleService->list($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }   
    
    public function info()
    {       
        try {     
            $params = $this->request->param();       
            $data = $this->singleService->info($params['id']);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    } 

    public function infoBySn()
    {       
        try {     
            $params = $this->request->param();
            $data = $this->singleService->infoBySn($params['sn']);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    } 
    
    public function save()
    {       
        try {    
            $params = $this->request->param(); 
            if(empty($params['operator_id'])){
                $params['operator_id'] = $this->request->userInfo->id;
            }            
            $data = $this->singleService->save($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function delete()
    {       
        try {    
            $params = $this->request->param();  
            $data = $this->singleService->delete($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }

    public function csv()
    {       
        try {          
            $params = $this->request->param();             
            $data = $this->singleService->csv($params);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }

        return $this->success($data);
    }
}
